<?php

namespace App\Http\Controllers\Admin;

use App\Models\ShenpiDetail;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\AttendanceUpdateRequest;
use Carbon\Carbon;
use App\Models\Attendance;
use App\Models\User;
use App\Models\AttendanceSet;
use App\Models\RoleUser;
use App\Models\Roles;
use App\Models\Group;
use Illuminate\Support\Facades\Input;
class AttendanceController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $active = Input::get('active',0);


        $tongxunlu=$this->getTongxunlu();

        if(is_mobile()){
            $view = 'admin.attendance.index';
        }else{
            $view = 'pc_admin.attendance.index';
        }
        return view($view)
            ->with('company',$this->company)
            ->with('tongxunlu',$tongxunlu)
            ->with('roleArray',$this->roleArray)
            ->with('user',$this->user)
            ->with('active',$active);

    }


    public function getTongxunlu()
    {
        $tongxunlu = array();
        $groups = Group::where('corpid',$this->corpid)->select('id','group_name','fid','class_id')->get();
        $groups = $groups->toArray();
        $groups_add = array('id'=>0,'group_name'=>'我的同事','fid'=>'undefined','class_id'=>1);
        array_unshift($groups,$groups_add);
        $rol=RoleUser::where('user_id',$this->user->id)->where("role_id",10)->first();
         if ($rol==null){
             $scope=false;
         }else{
             $scope=unserialize($rol->scope);
         }

        if($scope==false){
            $users = User::where('corpid',$this->corpid)->where('status','1')->orderBy('is_leader','desc')->orderByRaw('CONVERT (kh_users.qw_name USING gbk)')->select('id','qw_userid','user_logo','qw_name','group_id','is_leader','is_sales')->get();
        }else{

            $users = User::wherein('id',$scope)->where('status','1')->orderBy('is_leader','desc')->orderByRaw('CONVERT (kh_users.qw_name USING gbk)')->select('id','qw_userid','user_logo','qw_name','group_id','is_leader','is_sales')->get();
        }
        foreach ($users as $u){
            $u->role = RoleUser::where('corpid',$this->corpid)->where('user_id',$u->id)->lists('role_id');
        }
        $users = $users->toArray();
        foreach ($groups as &$g){
            $g['member'] = $this->filterUser($g['id'],$users);
            if($g['fid'] == 0){
                $tongxunlu[] = $g;
            }
        }
        foreach ($tongxunlu as $k=>&$t){
            if($t['id'] !=0){
                $t['child'] = $this->filterGroup($t['id'],$groups);
                foreach ($t['child'] as &$cld){
                    $cld['child'] = $this->filterGroup($cld['id'],$groups);
                }
            }
        }
        return $tongxunlu;
    }

    //筛选用户
    private function filterUser($group_id,$users){
        $return = array();
        foreach ($users as $u){
            if($u['group_id'] == $group_id){
                $return[] = $u;
            }
        }
        return $return;
    }

    //筛选组
    private function filterGroup($group_id,$groups){
        $return = array();
        foreach ($groups as $u){
            if($u['fid'] == $group_id){
                $return[] = $u;
            }
        }
        return $return;
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $time= Carbon::now()->toDateString();
        $event=$this->getEvent($time,$id);

        $count=$this->getEventscore($time,$id);

        //  dd($count);
        $user=User::find($id);

        if(is_mobile()){
            $view = 'admin.attendance.show';
        }else{
            $view = 'pc_admin.attendance.show';
        }
        return view($view)->with('user',$user)->with('id',$id)->with('count',$count)->with('this_user',$this->user)->with('event',json_encode($event));
    }

    protected function getEvent($time,$id){
        $start = strtotime ('first day of '.$time);
        $end = strtotime ('last day of '.$time);
        $data=Attendance::where(['user_id'=>$id])->whereBetween('time', [$start ,  $end])->get();
        $event=[];
        foreach ( $data as $value){
            $event[$value->time]=[];
            $Q1= ($value->late||$value->early||$value->absenteeism||(float)$value->business||(float)$value->sick || $value->chuchai || $value->qita);
            $Q2= $value->public ;
            $Q3=$value->overtime ;
            $Q1&&$Q2&&$Q3&&array_push($event[$value->time],6);
            !$Q1&&$Q2&&$Q3&&array_push($event[$value->time],5);
            $Q1&&!$Q2&&$Q3&&array_push($event[$value->time],4);
            $Q1&&$Q2&&!$Q3&&array_push($event[$value->time],3);
            !$Q1&&!$Q2&&$Q3&&array_push($event[$value->time],2);
            !$Q1&&$Q2&&!$Q3&&array_push($event[$value->time],1);
            $Q1&&!$Q2&&!$Q3&&array_push($event[$value->time],0);
        }

        return  $event;
    }

    protected function getEventscore($time,$id){
        $start = strtotime ('first day of '.$time);
        $end = strtotime ('last day of '.$time);
        $data=Attendance::where(['user_id'=>$id])->whereBetween('time', [$start ,  $end])->get();
        $score=0;
        foreach ( $data as $value){
            $score+=$value->score;
        }

        return  $score;
    }

    public function getDateState(Request $request,$id){

        $time=$request->input('time');
        $time=date("Y-m-d",$time);
        $event=$this->getEvent($time,$id);
        return  $this->formatJson('success','',$event);
        
    }

     public function ajaxFrom(Request $request){
         $time=$request->input('time');
         $id=$request->input('id');
         $data=Attendance::where(['user_id'=>$id])->where('time',$time)->first();
         if(is_mobile()){
             $view = 'admin.attendance.ajax_date';
         }else{
             $view = 'pc_admin.attendance.ajax_date';
         }
         return view($view)->with('id',$id)->with('info',$data);
     }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AttendanceUpdateRequest $request, $id)
    {
        //


        $data=$request->FillData();
        $time=$data['time'];
        //dd($time);
        $set=AttendanceSet::where('corpid',$this->corpid)->select('late','early','absenteeism','business','sick','overtime')->first()->toArray();
        //分数计算
        $score=0;
        foreach ($set as $key=>$value){
           $score+=$data[$key]*$value;
        }
        $data['score']=$score;
        $data['year_param'] = date('Y',$time);
        $data['month_param'] = date('Ym',$time);
        $data['date_param'] = date('Ymd',$time);
        $Attendance=Attendance::firstOrCreate(['time'=>$time,'corpid'=>$this->corpid,'user_id'=>$id]);
        
        if($data['business'] != $Attendance->business){
            //事假修改
            $shenpi = ShenpiDetail::where('corpid',$this->corpid)->where('from_user_id',$id)->where('kind',2)->where('date_param',$Attendance->date_param)->where('is_modified',0)->first();
            if(!empty($shenpi)){
                $shenpi->is_modified = 1;
                $shenpi->save();

                $newshenpi = new ShenpiDetail();
                $newshenpi->corpid = $shenpi->corpid;
                $newshenpi->year_param = $shenpi->year_param;
                $newshenpi->month_param = $shenpi->month_param;
                $newshenpi->date_param = $shenpi->date_param;
                $newshenpi->kind = $shenpi->kind;
                $newshenpi->from_user_id = $shenpi->from_user_id;
                $newshenpi->description = $shenpi->description;
                $newshenpi->is_notify = 1;
                if($data['business'] == 0){
                    $newshenpi->is_empty = 1;
                }else{
                    $newshenpi->is_empty = 0;
                    $newshenpi->new_count = $data['business'];
                }
                $newshenpi->save();
            }
        }
        if($data['sick'] != $Attendance->sick){
            //病假修改
            $shenpi = ShenpiDetail::where('corpid',$this->corpid)->where('from_user_id',$id)->where('kind',3)->where('date_param',$Attendance->date_param)->where('is_modified',0)->first();
            if(!empty($shenpi)){
                $shenpi->is_modified = 1;
                $shenpi->save();

                $newshenpi = new ShenpiDetail();
                $newshenpi->corpid = $shenpi->corpid;
                $newshenpi->year_param = $shenpi->year_param;
                $newshenpi->month_param = $shenpi->month_param;
                $newshenpi->date_param = $shenpi->date_param;
                $newshenpi->kind = $shenpi->kind;
                $newshenpi->from_user_id = $shenpi->from_user_id;
                $newshenpi->description = $shenpi->description;
                $newshenpi->is_notify = 1;
                if($data['sick'] == 0){
                    $newshenpi->is_empty = 1;
                }else{
                    $newshenpi->is_empty = 0;
                    $newshenpi->new_count = $data['sick'];
                }
                $newshenpi->save();
            }
        }
        if($data['public'] != $Attendance->public){
            //年假婚假修改
            if($Attendance->public == 1){
                //年假被改
                $shenpi = ShenpiDetail::where('corpid',$this->corpid)->where('from_user_id',$id)->where('kind',4)->where('date_param',$Attendance->date_param)->where('is_modified',0)->first();
                if(!empty($shenpi)){
                    $shenpi->is_modified = 1;
                    $shenpi->save();

                    $newshenpi = new ShenpiDetail();
                    $newshenpi->corpid = $shenpi->corpid;
                    $newshenpi->year_param = $shenpi->year_param;
                    $newshenpi->month_param = $shenpi->month_param;
                    $newshenpi->date_param = $shenpi->date_param;
                    $newshenpi->kind = $shenpi->kind;
                    $newshenpi->from_user_id = $shenpi->from_user_id;
                    $newshenpi->description = $shenpi->description;
                    $newshenpi->is_notify = 1;
                    if($data['public'] != $Attendance->public){
                        $newshenpi->is_empty = 1;
                    }else{
                        $newshenpi->is_empty = 0;
                        $newshenpi->new_count = $data['public'];
                    }
                    $newshenpi->save();
                }
            }elseif ($Attendance->public == 2){
                //婚假被改
                $shenpi = ShenpiDetail::where('corpid',$this->corpid)->where('from_user_id',$id)->where('kind',5)->where('date_param',$Attendance->date_param)->where('is_modified',0)->first();
                if(!empty($shenpi)){
                    $shenpi->is_modified = 1;
                    $shenpi->save();

                    $newshenpi = new ShenpiDetail();
                    $newshenpi->corpid = $shenpi->corpid;
                    $newshenpi->year_param = $shenpi->year_param;
                    $newshenpi->month_param = $shenpi->month_param;
                    $newshenpi->date_param = $shenpi->date_param;
                    $newshenpi->kind = $shenpi->kind;
                    $newshenpi->from_user_id = $shenpi->from_user_id;
                    $newshenpi->description = $shenpi->description;
                    $newshenpi->is_notify = 1;
                    if($data['public'] != $Attendance->public){
                        $newshenpi->is_empty = 1;
                    }else{
                        $newshenpi->is_empty = 0;
                        $newshenpi->new_count = $data['public'];
                    }
                    $newshenpi->save();
                }

            }
        }
        if($data['chuchai'] != $Attendance->chuchai){
            //出差修改
            $shenpi = ShenpiDetail::where('corpid',$this->corpid)->where('from_user_id',$id)->where('kind',1)->where('date_param',$Attendance->date_param)->where('is_modified',0)->first();
            if(!empty($shenpi)){
                $shenpi->is_modified = 1;
                $shenpi->save();

                $newshenpi = new ShenpiDetail();
                $newshenpi->corpid = $shenpi->corpid;
                $newshenpi->year_param = $shenpi->year_param;
                $newshenpi->month_param = $shenpi->month_param;
                $newshenpi->date_param = $shenpi->date_param;
                $newshenpi->kind = $shenpi->kind;
                $newshenpi->from_user_id = $shenpi->from_user_id;
                $newshenpi->description = $shenpi->description;
                $newshenpi->is_notify = 1;
                if($data['chuchai'] == 0){
                    $newshenpi->is_empty = 1;
                }else{
                    $newshenpi->is_empty = 0;
                    $newshenpi->new_count = $data['chuchai'];
                }
                $newshenpi->save();
            }
        }
        if($data['qita'] != $Attendance->qita){
            //其他修改
            $shenpi = ShenpiDetail::where('corpid',$this->corpid)->where('from_user_id',$id)->where('kind',6)->where('date_param',$Attendance->date_param)->where('is_modified',0)->first();
            if(!empty($shenpi)){
                $shenpi->is_modified = 1;
                $shenpi->save();

                $newshenpi = new ShenpiDetail();
                $newshenpi->corpid = $shenpi->corpid;
                $newshenpi->year_param = $shenpi->year_param;
                $newshenpi->month_param = $shenpi->month_param;
                $newshenpi->date_param = $shenpi->date_param;
                $newshenpi->kind = $shenpi->kind;
                $newshenpi->from_user_id = $shenpi->from_user_id;
                $newshenpi->description = $shenpi->description;
                $newshenpi->is_notify = 1;
                if($data['qita'] == 0){
                    $newshenpi->is_empty = 1;
                }else{
                    $newshenpi->is_empty = 0;
                    $newshenpi->new_count = $data['qita'];
                }
                $newshenpi->save();
            }
        }
        
        $Attendance->update($data);

        return  $this->formatJson('success','修改成功',$Attendance);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
